'use strict';

const Controller = require('egg').Controller;
let jwt = require("jsonwebtoken")

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, egg';
  }

  //登录
  async login() {
    const { app, ctx } = this
    const { username, password } = ctx.request.body
    const data = await app.mysql.query(`select * from login where username = "${username}" and password = "${password}"`)

    //遍历登录成功的数据存入到datas
    const datas = data.map(item => {
      return {
        username: item.username,
        url: item.url,
        name: item.name,
        id: item.id
      }
    })

    if (data.length) {
      //登陆成功传入datas
      ctx.body = {
        code: 1,
        datas,
        token: jwt.sign({ ...data }, '1901A', { expiresIn: '4m' }),
      }
    } else {
      ctx.body = {
        code: 0,
      }
    }
  }

  //注册
  async register() {
    const { app, ctx } = this
    const { username, password } = ctx.request.body
    const data = await app.mysql.get('login', { username })

    if (!data && username !== '' && password !== '') {
      app.mysql.insert('login', { username, password })

      ctx.body = {
        code: 1,
        data
      }
    } else {
      ctx.body = {
        code: 0,
        data
      }
    }
  }

  //渲染买房数据
  async buyHouse() {
    const { app, ctx } = this
    const data = await app.mysql.select('buyHouse')
    const { authorization } = ctx.request.header

    try {
      jwt.verify(authorization, '1901A')
      ctx.body = {
        code: 1,
        msg: "成功",
        data
      }
    } catch (error) {
      ctx.body = {
        code: 0,
        msg: '登录失效，请重新登录'
      }
    }
  }



  //渲染租房数据
  async rentHouse() {
    const { app, ctx } = this
    const data = await app.mysql.select('rentHouse')

    const { authorization } = ctx.request.header

    try {
      jwt.verify(authorization, '1901A')
      ctx.body = {
        code: 1,
        msg: "成功",
        data
      }
    } catch (error) {
      ctx.body = {
        code: 0,
        msg: '登录失效，请重新登录'
      }
    }
  }

  //获取详情数据
  async Detail() {
    const { app, ctx } = this
    const { id } = ctx.request.body
    const data = await app.mysql.get('buyHouse', { id })

    ctx.body = {
      code: 1,
      data
    }
  }

   //获取租房详情数据
   async Detail2() {
    const { app, ctx } = this
    const { id } = ctx.request.body
    const data = await app.mysql.get('rentHouse', { id })

    ctx.body = {
      code: 1,
      data
    }
  }

  //户型筛选
  async screening3() {
    const { app, ctx } = this
    const { value } = ctx.request.body
    const data = await app.mysql.select("buyHouse", { where: { type: value } })

    if (value === '户型') {
      ctx.body = {
        code: 1,
        data
      }
    } else {
      ctx.body = {
        code: 500,
        data
      }
    }
  }

  //租房户型筛选
  async rentScreening3() {
    const { app, ctx } = this
    const { value } = ctx.request.body
    const data = await app.mysql.select("rentHouse", { where: { type: value } })

    if (value === '户型') {
      ctx.body = {
        code: 1,
        data
      }
    } else {
      ctx.body = {
        code: 500,
        data
      }
    }
  }

  //朝向筛选
  async screening4() {
    const { app, ctx } = this
    const { value } = ctx.request.body
    const data = await app.mysql.select("buyHouse", { where: { direction: value } })

    if (value === '朝向') {
      ctx.body = {
        code: 1,
        data
      }
    } else {
      ctx.body = {
        code: 500,
        data
      }
    }
  }

  //租房朝向筛选
  async rentScreening4() {
    const { app, ctx } = this
    const { value } = ctx.request.body
    const data = await app.mysql.select("rentHouse", { where: { direction: value } })

    if (value === '朝向') {
      ctx.body = {
        code: 1,
        data
      }
    } else {
      ctx.body = {
        code: 500,
        data
      }
    }
  }

    //搜索买房数据
    async buyCheck() {
      const { app, ctx } = this
      const { value } = ctx.request.body
      const data = await app.mysql.query(`select * from buyHouse where tit like "%${ value }%"`)
      console.log(data);
      
      
      ctx.body = {
        code:1,
        data
      }
    }

}

module.exports = HomeController;
